package org.catdroid.englishformusicians;

import com.google.gson.Gson;

import android.content.Context;
import android.preference.PreferenceManager;

public class TestRepository {

	public static final String PREF_REPOSITORY = "prefTestRepository";

	private static StringList CACHED = null;
	private static final Gson GSON = new Gson();

	public static StringList getTestRepository(Context c) {
		if (CACHED == null) {
			String json = PreferenceManager.getDefaultSharedPreferences(c).getString(PREF_REPOSITORY, null);
			if (json != null) {
				CACHED = GSON.fromJson(json, StringList.class);
			} else {
				CACHED = new StringList();
			}
		}
		return CACHED;
	}

	public static boolean isChecked(Context c, String text) {
		StringList knownTexts = getTestRepository(c);
		return knownTexts.contains(text);
	}

	public static void addChecked(Context c, String text) {
		StringList knownTexts = getTestRepository(c);
		knownTexts.add(text);
		CACHED = knownTexts;
		PreferenceManager.getDefaultSharedPreferences(c).edit().putString(PREF_REPOSITORY, GSON.toJson(knownTexts)).commit();
	}
}
